package com.mhkj.base.rbac.role.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.itemInfo.item.service.ItemService;
import com.mhkj.base.rbac.role.entity.SystemRole;
import com.mhkj.base.rbac.role.service.MyRoleService;
import com.mhkj.base.region.service.NationService;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.componet.SimpleCode;
import com.mics.core.common.exception.ErrorCode;
import com.mics.core.common.exception.OperateException;
import com.mics.core.common.util.json.JsonUtils;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 角色
 */
@Controller
@RequestMapping("/myRole")
public class MyRoleController extends BaseController<SystemRole> {
	@Autowired
	private MyRoleService roleService;
	@Autowired
	private NationService nationService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private ItemService itemService;
	@Autowired
	private DictionaryService dictionaryService;
	@Resource
	private SimpleCode simpleCode;
	@Resource
	private JsonHandler jsonHandler;
	/**
	 * 分页查询角色
	 * 
	 */
	@RequestMapping(value="/listByPage")
	@ResponseBody
	public void listByPage(HttpServletResponse response, SystemRole Role, int pageIndex, int pageSize ) {
		Pagination<SystemRole> pagination = roleService.listByPage(Role,pageIndex, pageSize);
		jsonHandler.sendJsonString(response, true,"获取数据成功", pagination, new String[]{"id","roleName","name","code"},
				"id","roleName","name","code");
	}

	/**
	 * 保存
	 * 
	 */
	@RequestMapping(value="/save")
	@ResponseBody
	public ResponseMessage save(SystemRole role) {
		String code = simpleCode.generatorSimpleCode(SystemRole.class, "code");
		role.setCode(code);
		String message = roleService.save(role);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 修改
	 * 
	 */
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage edit(SystemRole role) {
		SystemRole systemRoled =roleService.getById(role.getId());
		role.setCode(systemRoled.getCode());
		String message = roleService.edit(role);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 删除
	 * 
	 */
	@RequestMapping(value="/del")
	@ResponseBody
	public ResponseMessage del(SystemRole role) {
		try{
			String message = roleService.del(role);
			return ResponseMessage.createMessage("ok".equals(message), message);
		}catch(Exception e){
			throw new OperateException(ErrorCode.DELETE, "删除角色失败，角色已经被使用");
		}
		
	}

	/**
	 * 查询所有角色
	 * 
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public void listAll(HttpServletResponse response) {
		List<SystemRole> list = roleService.findAll();
		JsonUtils.sendJsonFromList(response, true,"获取数据成功", list, new String[]{"id","roleName","name","code"},
				"id","roleName","name","code");
	}
	
	
	/**
	 * 查询角色信息
	 */
	@RequestMapping(value="/info")
	@ResponseBody
	public void info(HttpServletResponse response,@RequestParam(value = "id", required = true) long id) {
		SystemRole role = roleService.getById(id);
		jsonHandler.sendJsonString(response, true,"获取数据成功", role, new String[]{"id","roleName","name","code"},
				"id","roleName","name","code");
		}

}
